// pages/store/store.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    freightnav: [
      { txt: '我的产品' },
      { txt: '发布产品' },
    ],
    navindex:0,
    xphoto: '/images/unp_01.png',
    company_logo:'/images/unp_01.png',
    none_login:true,
    currentPage: 1,
    totalPage: 1,
    dataList:[],
    firstPath:'',
    chanpingArr:[],
    chandiArr:[],
    uData:[]
   
  },
  coalde(e){
    var id = e.currentTarget.dataset.id;
    app.navTo('../coalde/coalde?id='+id)
  },
  //卖家认证
  seller(){
    app.navTo('../sellercert/sellercert')
  },
  //地址
  start_ads: function (e) {
    wx.openLocation({
      latitude: 23.362490,
      longitude: 116.715790,
      scale: 18,
      name: '华乾大厦',
      address: '金平区长平路93号'
    })
  },
  //拨打电话
  start_phone: function () {
    wx.makePhoneCall({
      phoneNumber: '1340000'
    })
  },
  submit:function(e){
  
    var param = e.detail.value;
    param.chanpinglunbotu = this.data.chanpingArr;
    param.chandilunbotu = this.data.chandiArr;
    param.company_logo = this.data.company_logo;
    var that = this;
    wx.showLoading({})
    util.commonRequest('publishProduct', 'post', param, function (res) {
      wx.hideLoading()

      if (res.data.code == 200) {
        wx.showToast({
          icon:'success',
          title: res.data.msg
        })
         
        that.setData({
          navindex: 0,
          currentPage: 1,
          totalPage: 1,
          dataList: [],
        })
        that.initData();
        
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });
  },
  xphotos: function () {
    util.upload_img(3).then(res=>{
       
    })
  },
  xphotos1:function(){
    var that = this;
    util.upload_img().then(res => {
     that.setData({
       company_logo:res[0]
     })
    })
  },
  xphotos2:function(){
    var that = this;
  
    util.upload_img().then(res => {
      let len1 = res.length;
      let len2 = that.data.chanpingArr.length;
      if ((len1 + len2) > 3){
        wx.showToast({
          title: '最多上次3张图片'
        })
        return;
      }else{
        let tempArr = that.data.chanpingArr;
        let tempD = tempArr.concat(res);
        that.setData({
          chanpingArr: tempD
        })
      }
     
    })
  },
  xphotos3: function () {
    var that = this;

    util.upload_img().then(res => {
      let len1 = res.length;
      let len2 = that.data.chandiArr.length;
      if ((len1 + len2) > 3) {
        wx.showToast({
          title: '最多上次3张图片'
        })
        return;
      } else {
        let tempArr = that.data.chandiArr;
        let tempD = tempArr.concat(res);
        that.setData({
          chandiArr: tempD
        })
      }

    })
  },
  initData:function(){
    var that = this;
    var param = {
      currentPage: that.data.currentPage
    };
  
    wx.showLoading({})
    util.commonRequest('product', 'post', param, function (res) {
      wx.hideLoading()
      if (res.data.code == 148 ){//你不是货主
        app.navTo('../index/index')
        return;
      } else if (res.data.code == 147) {//货主认证
        app.navTo('../shippercert/shippercert')
        return;
      } else if (res.data.code == 149) {//货主卖家认证
        app.navTo('../sellercert/sellercert')
        return;
      }
      else if (res.data.code == 200) {
        var tempArr = res.data.data.data;
        var oriArr = that.data.dataList;
        var finalyArr = oriArr.concat(tempArr);
        that.setData({
          dataList: finalyArr,
          totalPage: res.data.data.last_page
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  click: function (e) {
    var id = e.currentTarget.dataset.id;
     
    this.setData({
      navindex: id,
      currentPage: 1,
      totalPage: 1,
      dataList: [],
    })
    this.initData(); 
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    util.commonRequest('getuserInfoByToken', 'post', {}, function (res) {
      if (res.data.code == 200) {
        that.setData({
          uData: res.data.data
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;

    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.initData();
    } else {
      console.log("已经是全部数据")
      // wx.showToast({
      //   'icon':'loading',
      //   title: '已经是全部数据了',
      // })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})